/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Islandgroup;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Localityelement;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class IslandgroupJpaController {

    public IslandgroupJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Islandgroup save(Islandgroup islandgroup) {
        Islandgroup aux = findIslandgroup(islandgroup.getIslandgroup());
        if(aux==null)
            return create(islandgroup);
        else
            return aux;
    }

    public Islandgroup create(Islandgroup islandgroup) {
        Islandgroup r = null;
        if (islandgroup.getLocalityelementList() == null) {
            islandgroup.setLocalityelementList(new ArrayList<Localityelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Localityelement> attachedLocalityelementList = new ArrayList<Localityelement>();
            for (Localityelement localityelementListLocalityelementToAttach : islandgroup.getLocalityelementList()) {
                localityelementListLocalityelementToAttach = em.getReference(localityelementListLocalityelementToAttach.getClass(), localityelementListLocalityelementToAttach.getIdlocalityelement());
                attachedLocalityelementList.add(localityelementListLocalityelementToAttach);
            }
            islandgroup.setLocalityelementList(attachedLocalityelementList);
            r = em.merge(islandgroup);
            for (Localityelement localityelementListLocalityelement : islandgroup.getLocalityelementList()) {
                Islandgroup oldIslandgroupOfLocalityelementListLocalityelement = localityelementListLocalityelement.getIslandgroup();
                localityelementListLocalityelement.setIslandgroup(islandgroup);
                localityelementListLocalityelement = em.merge(localityelementListLocalityelement);
                if (oldIslandgroupOfLocalityelementListLocalityelement != null) {
                    oldIslandgroupOfLocalityelementListLocalityelement.getLocalityelementList().remove(localityelementListLocalityelement);
                    oldIslandgroupOfLocalityelementListLocalityelement = em.merge(oldIslandgroupOfLocalityelementListLocalityelement);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Islandgroup islandgroup) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Islandgroup persistentIslandgroup = em.find(Islandgroup.class, islandgroup.getIdislandgroup());
            List<Localityelement> localityelementListOld = persistentIslandgroup.getLocalityelementList();
            List<Localityelement> localityelementListNew = islandgroup.getLocalityelementList();
            List<Localityelement> attachedLocalityelementListNew = new ArrayList<Localityelement>();
            for (Localityelement localityelementListNewLocalityelementToAttach : localityelementListNew) {
                localityelementListNewLocalityelementToAttach = em.getReference(localityelementListNewLocalityelementToAttach.getClass(), localityelementListNewLocalityelementToAttach.getIdlocalityelement());
                attachedLocalityelementListNew.add(localityelementListNewLocalityelementToAttach);
            }
            localityelementListNew = attachedLocalityelementListNew;
            islandgroup.setLocalityelementList(localityelementListNew);
            islandgroup = em.merge(islandgroup);
            for (Localityelement localityelementListOldLocalityelement : localityelementListOld) {
                if (!localityelementListNew.contains(localityelementListOldLocalityelement)) {
                    localityelementListOldLocalityelement.setIslandgroup(null);
                    localityelementListOldLocalityelement = em.merge(localityelementListOldLocalityelement);
                }
            }
            for (Localityelement localityelementListNewLocalityelement : localityelementListNew) {
                if (!localityelementListOld.contains(localityelementListNewLocalityelement)) {
                    Islandgroup oldIslandgroupOfLocalityelementListNewLocalityelement = localityelementListNewLocalityelement.getIslandgroup();
                    localityelementListNewLocalityelement.setIslandgroup(islandgroup);
                    localityelementListNewLocalityelement = em.merge(localityelementListNewLocalityelement);
                    if (oldIslandgroupOfLocalityelementListNewLocalityelement != null && !oldIslandgroupOfLocalityelementListNewLocalityelement.equals(islandgroup)) {
                        oldIslandgroupOfLocalityelementListNewLocalityelement.getLocalityelementList().remove(localityelementListNewLocalityelement);
                        oldIslandgroupOfLocalityelementListNewLocalityelement = em.merge(oldIslandgroupOfLocalityelementListNewLocalityelement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = islandgroup.getIdislandgroup();
                if (findIslandgroup(id) == null) {
                    throw new NonexistentEntityException("The islandgroup with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Islandgroup islandgroup;
            try {
                islandgroup = em.getReference(Islandgroup.class, id);
                islandgroup.getIdislandgroup();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The islandgroup with id " + id + " no longer exists.", enfe);
            }
            List<Localityelement> localityelementList = islandgroup.getLocalityelementList();
            for (Localityelement localityelementListLocalityelement : localityelementList) {
                localityelementListLocalityelement.setIslandgroup(null);
                localityelementListLocalityelement = em.merge(localityelementListLocalityelement);
            }
            em.remove(islandgroup);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Islandgroup> findIslandgroupEntities() {
        return findIslandgroupEntities(true, -1, -1);
    }

    public List<Islandgroup> findIslandgroupEntities(int maxResults, int firstResult) {
        return findIslandgroupEntities(false, maxResults, firstResult);
    }

    private List<Islandgroup> findIslandgroupEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Islandgroup.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Islandgroup findIslandgroup(String q) {
        Query query= getEntityManager().createNamedQuery("Islandgroup.findByIslandgroup");
        query.setParameter("islandgroup",q);

        return query.getResultList().isEmpty()?null:(Islandgroup)query.getResultList().get(0);
    }

    public Islandgroup findIslandgroup(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Islandgroup.class, id);
        } finally {
            em.close();
        }
    }

    public int getIslandgroupCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Islandgroup> rt = cq.from(Islandgroup.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
